package com.wxproj.wxai.controller;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.Claim;
import com.baomidou.mybatisplus.extension.api.R;
import com.wxproj.wxai.entity.UserPic;
import com.wxproj.wxai.mapper.UserPicMapper;
import com.wxproj.wxai.utils.AiManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping
public class UserPicController {
    @Resource
    private AiManager aiManager;
    @Autowired
    private UserPicMapper userPicMapper;

    @PostMapping("/picai")
    public R picai(@RequestBody String content, HttpServletRequest httpServletRequest) throws Exception {
        String token = httpServletRequest.getHeader("Authorization").toString();
        Claim claim = JWT.decode(token).getClaim("id");
        String id = claim.toString().replaceAll("\"","");
        String vals = aiManager.testInvoke(content);
        String title = vals.split("https://")[1];
        String con = title.split(".png")[0];
        String url = "https://"+con+".png";

        UserPic userPic = new UserPic();
        userPic.setUser_id(id);
        userPic.setPic_url(url);
        userPic.setCreate_time(new Date());
        System.out.println(id);
        userPicMapper.insert(userPic);
        return R.ok(vals);
    }
}
